package top.continew.admin.webapi.jobfair;

import org.springframework.validation.annotation.Validated;
import top.continew.admin.common.model.dto.LoginUser;
import top.continew.admin.common.util.helper.LoginHelper;
import top.continew.starter.extension.crud.enums.Api;

import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.web.bind.annotation.*;

import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
import top.continew.starter.extension.crud.controller.BaseController;
import top.continew.admin.jobfair.model.query.CompanyQuery;
import top.continew.admin.jobfair.model.req.CompanyReq;
import top.continew.admin.jobfair.model.resp.CompanyDetailResp;
import top.continew.admin.jobfair.model.resp.CompanyResp;
import top.continew.admin.jobfair.service.CompanyService;
import top.continew.starter.extension.crud.util.ValidateGroup;
import top.continew.starter.web.model.R;

/**
 * 企业信息管理 API
 *
 * @author xiahaike
 * @since 2024/09/10 20:58
 */
@Tag(name = "企业信息管理 API")
@RestController
@CrudRequestMapping(value = "/jobfair/company", api = {Api.PAGE, Api.GET, Api.ADD, Api.UPDATE, Api.DELETE, Api.EXPORT})
public class CompanyController extends BaseController<CompanyService, CompanyResp, CompanyDetailResp, CompanyQuery, CompanyReq> {

    @Override
    @ResponseBody
    @PostMapping
    public R<Long> add(@RequestBody CompanyReq req) {
        this.checkPermission(Api.ADD);
        // 查看该用户是否注册过
        LoginUser loginUser = LoginHelper.getLoginUser();
        Long id = loginUser.getId();
        if (this.baseService.checkDuplicate(req, id)) {
            return R.fail("已注册过企业信息，请勿重复注册");
        }
        return R.ok("新增成功", this.baseService.add(req));
    }

    // 根据用户id查询企业id
    @GetMapping("/getCompanyId")
    public R<Long> getCompanyId() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        Long id = loginUser.getId();
        return R.ok(this.baseService.getCompanyId(id));
    }
}